//
//  StaticPickerView.m
//  AgriMobile
//
//  Created by 春名 啓作 on 11/06/12.
//  Copyright 2011 e-agri. All rights reserved.
//

#import "StaticPickerController.h"


@implementation StaticPickerController

@synthesize dataCaptions;
@synthesize dataValues;
@synthesize srcLabelCaption;
@synthesize srcLabelValue;

- (void)viewDidLoad {
    [super viewDidLoad];

	piv.delegate = self;
	piv.dataSource = self;
	piv.showsSelectionIndicator = YES;

	dataMap = [[NSDictionary dictionaryWithObject:dataValues forKey:dataCaptions] retain];
	NSLog(@"dataMap = %@", dataMap);
	
	if (srcLabelValue != nil) {
		NSLog(@"currentValue = %@", srcLabelValue.text);
		
		for(int i=0; i<[dataValues count]; i++){
			if ([srcLabelValue.text isEqualToString:[dataValues objectAtIndex:i]]) {
				[piv selectRow:i inComponent:0 animated:NO];
				NSLog(@"selectRow[%d] = %@", i, [dataValues objectAtIndex:i]);
				break;
			}
		}
	}
}

- (void)viewWillAppear:(BOOL)animated {

}

- (IBAction)btnCancelClicked {
	[self.view removeFromSuperview];
}

- (IBAction)btnCommitClicked {
	NSInteger row = [piv selectedRowInComponent:0];
	[srcLabelCaption setText:[dataCaptions objectAtIndex:row]];
	[srcLabelValue setText:[dataValues objectAtIndex:row]];
	
	NSLog(@"btnCommit at Row[%d] = %@", row, [dataValues objectAtIndex:row]);
	
	[self.view removeFromSuperview];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {

	return 1;
}

-(NSInteger)pickerView:(UIPickerView*)pickerView
	numberOfRowsInComponent:(NSInteger)component{

	//return [[dataMap allKeys] count];
	return [dataCaptions count];
}

-(NSString*)pickerView:(UIPickerView*)pickerView
		   titleForRow:(NSInteger)row forComponent:(NSInteger)component{

	//return [[dataMap allKeys] objectAtIndex:row];
	return [dataCaptions objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView
			didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

	/*
	[srcLabelCaption setText:[dataCaptions objectAtIndex:row]];
	[srcLabelValue setText:[dataMap objectForKey:[dataCaptions objectAtIndex:row]]];
	 */
}

- (void)dealloc {
    [super dealloc];
	
	NSLog(@"StaticPickerController dealloc");

	[dataMap release];
	[dataCaptions release];
	[dataValues release];
	[srcLabelValue release];
	[srcLabelCaption release];
}


@end
